--------------------- ******************************* NTO_SW Dati  SW 

select * from (
select distinct
--CODICE_CONTRATTO, STATO_CONTRATTO, MOVIMENTO, periodo,
'SNM' "CodFlusso",
ud.PIVA "PIvaUtente",
PIvaDistributore  "PIvaDistributore",
ud.udd "CodContrDisp",
a.Pod "Pod",
--to_date(to_char(decode(movimento,'RASC',DataMisura-1,'RACC',DataMisura,DataMisura-1),'dd/mm/yyyy'),'dd/mm/yyyy') "DataMisura",
b.data "DataMisura",
Trattamento  "Trattamento",
Tensione "Tensione",
PotContrImp "PotContrImp",
PotDisp "PotDisp",
Ka "Ka",
Kr  "Kr",
Kp "Kp",
MatrAtt   "MatrAtt",
MatrRea "MatrRea",
MatrPot  "MatrPot",
to_char(DataInstMisAtt,'dd/mm/yyyy') "DataInstMisAtt",
to_char(DataInstMisRea,'dd/mm/yyyy') "DataInstMisRea",
to_char(DataInstMisPot,'dd/mm/yyyy') "DataInstMisPot",
CifreAtt  "CifreAtt",
CifreRea  "CifreRea",
CifrePot  "CifrePot",
GruppoMis "GruppoMis",
Forfait "Forfait",
Raccolta "Raccolta",
TipoDato "TipoDato",
Validato  "Validato",
decode (a.trattamento,'M' ,EaM,null)  "EaM",
decode (a.trattamento,'M' ,ErM,null) "ErM",
decode (a.trattamento,'M' ,PotM,null)"PotM",
decode (a.trattamento,'F' ,EaF1,null) "EaF1",
decode (a.trattamento,'F' ,EaF2,null) "EaF2",
decode (a.trattamento,'F' ,EaF3,null) "EaF3",
decode (a.trattamento,'F' ,ErF1,null) "ErF1",
decode (a.trattamento,'F' ,ErF2,null) "ErF2",
decode (a.trattamento,'F' ,ErF3,null) "ErF3",
decode (a.trattamento,'F' ,PotF1,null) "PotF1",
decode (a.trattamento,'F' ,PotF2,null) "PotF2",
decode (a.trattamento,'F' ,PotF3,null) "PotF3",


DataInizioPeriodo "DataInizioPeriodo",
decode (a.trattamento,'M' ,EaM_1,null) "EaM_1"

from asm_delib_6_12_001 a ,asm_delib_6_12_EXCEL b,asm_delibera_16208_udd ud
where a.pod=b.pod
and trattamento <> 'O'
and ud.UDD=b.dp
 and ((
nvl(EaMc,0) +
nvl(ErMc,0)+
nvl(PotMc,0)+
nvl(EaF1c,0) +
nvl(EaF2c,0) +
nvl(EaF3c,0)+
nvl(ErF1c,0) +
nvl(ErF2C,0) +
nvl(ErF3C ,0)+
nvl(PotF1c,0) +
nvl(PotF2 ,0)+
nvl(PotF3c,0) )>0 or movimento IN ('L','IA'))
and  a.DataMisura = (select distinct max(DataMisura)  from asm_delib_6_12_001 c where  a.pod=c.pod)

 )

order by "Pod","CodContrDisp",to_date("DataMisura",'dd/mm/yyyy')